sensitivity.yb <- function(data, col_Treat, value_Treat0, col_IV, col_DV, col_group) {
  library(tidyr) # Tidy Messy Data
  library(plyr) # Tools for Splitting, Applying and Combining Data
  df <- data
  
  if (!is.null(col_group)) {
    df <- unite(df, "group", all_of(col_group), sep = "_", remove = F)
    df$group <- as.factor(df$group)
    group <- unique(df$group)
  } else {
    df$group <- as.factor(1)
    group <- unique(df$group)
  } #合并确定分组
  
  #计算dx
  for (i in c(1:length(col_Treat))) {
    if (i == 1) {
      dx <- df[,col_IV[i]] / unique(df[df[,col_Treat[i]] == value_Treat0[i],col_IV[i]])
    } else {
      dx <- dx * df[,col_IV[i]] / unique(df[df[,col_Treat[i]] == value_Treat0[i],col_IV[i]])
    }
  }
  df$dx <- dx-1
  
  
  #计算dy
  data_list <- list()
  out_list <- list()
  Data <- data.frame()
  for (i in c(1:length(group))) {
    data_list[[i]] <- df[df$group == group[i],]
    out_list[[i]] <- df[df$group == group[i], c(col_Treat, "dx", col_group)]
    for (j in c(1:length(col_Treat))) {
      if(j == 1) {
        CK_row <- which(data_list[[i]][,col_Treat[j]] == value_Treat0[j])
      } else {
        CK_row <- intersect(CK_row, which(data_list[[i]][,col_Treat[j]] == value_Treat0[j]))
      }
    }
    for (j in c(1:length(col_DV))) {
      y0 <- mean(data_list[[i]][CK_row,col_DV[j]])
      dy <- (data_list[[i]][,col_DV[j]] - y0)/y0 *100
      out_list[[i]] <- cbind(out_list[[i]], dy)
      names(out_list[[i]])[names(out_list[[i]]) == "dy"] <- col_DV[j]
    }
    Data <- rbind(Data, out_list[[i]])
  }
  
  Data <- Data[Data$dx != 0,] #清除不必要的i=0的数据
  
  # 转置数据
  for (i in c(1:length(col_DV))) {
    FT = rep(col_DV[i], length(Data[,1]))
    Data2 <- cbind(FT, Data[,c(col_group, "dx", col_DV[i])])
    names(Data2) <- c("FT", col_group,"dx", "dy")
    if (i == 1) {
      SC <- Data2
    } else {
      SC <- rbind(SC, Data2)
    }
  }
  
  # 计算敏感度
  SC$SC <- SC$dy / SC$dx
  
  col_group <- c(col_group, "FT")
  n <- length(col_group)
  out <- list()
  out[[n+1]] <- SC
  while (n > 0) {
    out[[n]] <- ddply(out[[n+1]], eval(parse(text = paste(".(", paste(col_group[1:n], collapse = ", "), ")"))),
                      summarize, 
                      sc = mean(SC, na.rm = TRUE), 
                      N  = length(SC),
                      sd = sd(SC, na.rm = TRUE),
                      se = se.yb(SC))
    names(out[[n]])[names(out[[n]]) == "sc"] <- "SC"
    n <- n-1
  }
  out[[length(out)]] <- NULL
  return(out)
} #敏感度计算

#data输入的数据, col_Treat处理列名, value_Treat0处理对照的水平名, col_IV处理强度列名（自变量）, col_DV响应性状列名（因变量）, col_group分组列名（越后越先求和）